package com.sxtyxt.online.choice;

import android.content.ContentUris;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.QuickContactBadge;
import android.widget.TextView;

import com.sxtyxt.entity.Contact;
import com.sxtyxt.online.R;

import java.io.InputStream;
import java.util.List;

public class ContactListAdapter extends BaseAdapter {  
    private LayoutInflater inflater;  
    private List<Contact> list;
    private String[] sections; // 存储每个章节
    private Context ctx; // 上下文  
  
    public ContactListAdapter(Context context, List<Contact> list
        ) {
        this.ctx = context;  
        this.inflater = LayoutInflater.from(context);  
        this.list = list;  
        this.sections = new String[list.size()];
  

    }  
  
    @Override  
    public int getCount() {  
        return list.size();  
    }  
  
    @Override  
    public Object getItem(int position) {  
        return list.get(position);  
    }  
  
    @Override  
    public long getItemId(int position) {  
        return position;  
    }  
  
    public void remove(int position) {  
        list.remove(position);  
    }  
  
    @Override  
    public View getView(int position, View convertView, ViewGroup parent) {  
        ViewHolder holder;  
        if (convertView == null) {  
            convertView = inflater.inflate(R.layout.contact_list_item, null);
            holder = new ViewHolder();  
            holder.quickContactBadge = (QuickContactBadge) convertView  
                    .findViewById(R.id.qcb);  
            holder.name = (TextView) convertView.findViewById(R.id.name);
            holder.number = (TextView) convertView.findViewById(R.id.number);  
            convertView.setTag(holder);  
        } else {  
            holder = (ViewHolder) convertView.getTag();  
        }  
  
        Contact contact = list.get(position);
        String name = contact.getDesplayName();  
        String number = contact.getPhoneNum();  
        holder.name.setText(name);  
        holder.number.setText(number);  
        holder.quickContactBadge.assignContactUri(Contacts.getLookupUri(  
                contact.getContactId(), contact.getLookUpKey()));  
        if (0 == contact.getPhotoId()) {  
            holder.quickContactBadge.setImageResource(R.drawable.ic_launcher);
        } else {  
            Uri uri = ContentUris.withAppendedId(  
                    ContactsContract.Contacts.CONTENT_URI,  
                    contact.getContactId());  
            InputStream input = ContactsContract.Contacts  
                    .openContactPhotoInputStream(ctx.getContentResolver(), uri);  
            Bitmap contactPhoto = BitmapFactory.decodeStream(input);  
            holder.quickContactBadge.setImageBitmap(contactPhoto);  
        }  

        return convertView;  
    }  
  
    private static class ViewHolder {  
        QuickContactBadge quickContactBadge;  
        TextView name;
        TextView number;  
    }  
  

}  